﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Launcher{
    
    static class Program{
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        public static Settings S = new Settings();
        public static string[] loadSettings(){
            string[] Output = File.ReadAllLines("Settings.ini");
            
            return Output;
        }
        public static void parseSettings(string[] Input){
            //check each index of the ini file
            for (int Index = 0; Index < Input.Length; Index++){
                Debug.WriteLine(Index+":"+Input[Index]);
                //clean up and get the first word of the line
                string Word = getFirstWord(Input[Index]).ToLower();
                Debug.WriteLine("   :" + Word);
                //if that line (formated) is fullscreen then = should follow
                //along with true or false
                if (Word == "fullscreen"){
                    //split into a array at the =
                    string[] StringArray = Input[Index].Split('=');
                    //clean up the other side of the =
                    string tempString = StringArray[1];
                    //remove spaces
                    tempString = rmChar(tempString, ' ');
                    //can we parse a bool
                    bool Temp = false;
                    bool.TryParse(tempString, out Temp);
                    //can then parse else finish
                    if (Temp){
                        S.fullscreen = bool.Parse(tempString);
                        
                    }
                    Debug.WriteLine("       :" + tempString);
                }
                else if (Word == "bloom"){
                    string[] StringArray = Input[Index].Split('=');
                    string tempString = StringArray[1];
                    tempString = rmChar(tempString, ' ');

                    bool Temp = false;
                    bool.TryParse(tempString, out Temp);
                    if (Temp){
                        S.bloom = bool.Parse(tempString);
                    }
                    Debug.WriteLine("       :" + tempString);
                }
                else if (Word == "exe location"){
                    string[] StringArray = Input[Index].Split('=');
                    S.exeloc = StringArray[1];
                }
            }
        }
        public static string rmChar(string Input, char CharToRm){
            string Output = "";
            for (int Index = 0; Index < Input.Length; Index++){
                if (Input[Index] != CharToRm){
                    Output += Input[Index];
                }
            }
            return Output;
        }
        public static string getFirstWord(string Input){
            string LInput = Input.ToUpper();
            string Output = "";
            
            for (int Index = 0; Index < Input.Length; Index++){
                if (LInput[Index] >= 65 && LInput[Index] <= 90){
                    Output += Input[Index];
                }
                else{
                    break;
                }
            }
            return Output;
        }
        public static void saveSettings(){
            string[] Output = {
            "Exe Location ="+S.exeloc,
            "Fullscreen = "+S.fullscreen,
            "Bloom = "+S.bloom,
            };
            File.WriteAllLines("Settings.ini", Output);
        }
        [STAThread]
        static void Main(){
            string[] stringArray = loadSettings();
            parseSettings(stringArray);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    public struct Settings{
        public string exeloc;
        public bool fullscreen;
        public bool bloom;
        
    }
}
